// File System Class
// (c) jimon game studio

#include "CFileSystem.h"
#include "CFileBuffer.h"
#include "IVariables.h"

namespace je
{
	namespace core
	{
		IMPLEMENT_SINGLETON(FileSystem,JE_ENGINE_FUNCTION_CALL)

		//! Constructor
		CFileSystem::CFileSystem()
		{
			SetFileSystem(this);
		}

		//! Destructor
		CFileSystem::~CFileSystem()
		{
			SetFileSystem(NULL);
		}

		//! Read File
		IFileBuffer * CFileSystem::ReadFile(const jeStringd & FileName)
		{
			jeStringd WorkFileName = GetVariables()->GetVariableString("Enesea_WorkingDirectory") + FileName;
			
			CFileBuffer * ReadFileBuffer = NULL;
			JENEW(ReadFileBuffer,CFileBuffer(WorkFileName,true,false))

			if(ReadFileBuffer->IsOk())
				return ReadFileBuffer;
			else
			{
				ReadFileBuffer->Drop();
				return NULL;
			}
		}

		//! Read File
		IFileBuffer * CFileSystem::WriteFile(const jeStringd & FileName)
		{
			jeStringd WorkFileName = GetVariables()->GetVariableString("Enesea_WorkingDirectory") + FileName;

			CFileBuffer * ReadFileBuffer = NULL;
			JENEW(ReadFileBuffer,CFileBuffer(WorkFileName,false,true))
			return ReadFileBuffer;
		}
	}
}
